package com.yonyou.iuap.jxzy.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.iuap.base.web.BaseController;
import com.yonyou.iuap.jxzy.entity.DomesticPaper;
import com.yonyou.iuap.jxzy.entity.WastePaperHeadVO;
import com.yonyou.iuap.jxzy.service.DomesticPaperService;
import com.yonyou.iuap.jxzy.service.IandDFileService;
import com.yonyou.iuap.jxzy.service.WastePaperHeadService;
import com.yonyou.iuap.jxzy.utils.ExcelParam;
import com.yonyou.iuap.jxzy.utils.ExcelUtil;
import com.yonyou.iuap.mvc.type.SearchParams;
import com.yonyou.uap.ieop.security.exception.BusinessException;


@Controller
@RequestMapping(value="/file_up")
public class FileUpController extends BaseController{

	@Autowired
	private IandDFileService iandDFileService;
	
	
	@RequestMapping(value = "/up")
	@ResponseBody
	public String uplod(HttpServletRequest request,HttpServletResponse response){
		MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest)(request);
		Map<String, MultipartFile> fileMap = multiRequest.getFileMap();
		String parameter = multiRequest.getParameter("Path");
		String type = multiRequest.getParameter("type")!=null?multiRequest.getParameter("type"):"";
		JSONObject jsonObject = new JSONObject();
		try {
			String uploadFile = this.iandDFileService.uploadFile(fileMap, parameter,type);
			return uploadFile;
		} catch (FileNotFoundException e) {
			jsonObject.put("message", "服务器路径读取/写入失败");
			e.printStackTrace();
		} catch (IOException e) {
			jsonObject.put("message", "文件读取/写入失败");
			e.printStackTrace();
		} catch (BusinessException e) {
			jsonObject.put("message", e.getMessage());
			e.printStackTrace();
		}
		jsonObject.put("success", "error");
		return jsonObject.toString();

	}

	/**
	 * 导出选中数据
	 * @param json
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/download")
	@ResponseBody
	public Object download(@RequestBody String json,HttpServletRequest request,HttpServletResponse response){
		JSONObject jsonObject = new JSONObject();
		try {
			JSONObject parseObject = new JSONObject().parseObject(json);
			JSONArray jsonArray = parseObject.getJSONArray("exportData");
			Object exportType=parseObject.get("exportType");//导出节点类型 0：国内废纸称重 ，1:进口废纸称重
			this.iandDFileService.download(jsonArray,exportType,response);
		} catch (Exception e) {
			jsonObject.put("success", "error");
			jsonObject.put("message", "文件读取/写入失败");
			return jsonObject.toString();
		}
		
		return this.buildSuccess("导出成功！");
	}
	
	
	/**
	 * 导出全部数据及当前页数据
	 * @param pageRequest
	 * @param searchParams
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/downloadpage" ,method = {RequestMethod.GET})
	@ResponseBody
	public Object downloadpage(SearchParams searchParams,PageRequest pageRequest,HttpServletRequest request,HttpServletResponse response){
		JSONObject jsonObject = new JSONObject();
		try {
			Map<String, Object> searchMap=searchParams.getSearchMap();
			if(searchMap!=null && searchMap.size()>0){
				String exportType=String.valueOf(searchMap.get("exportType"));
				this.iandDFileService.downloadpage(searchParams,pageRequest,exportType,response);
			}
		} catch (Exception e) {
			jsonObject.put("success", "error");
			jsonObject.put("message", "文件读取/写入失败");
			return jsonObject.toString();
		}
		
		return this.buildSuccess("导出成功！");
	}
}
